﻿using System.Collections.Generic;
using Cinema.Services.Contracts;
using System.IO;
using System;
using Cinema.BL.Interfaces.DTOs;
using System.Text;
using System.ServiceModel.Web;
using Newtonsoft.Json;

namespace Cinema.Services
{
    public class CommonService : ServiceBase, ICommonService
    {

        //MOVIE
        public IEnumerable<BL.Interfaces.DTOs.MovieDTO> GetMovieList()
        {
            using (var managerFactory = GetFactorySession())
            {
                return managerFactory.MovieManager.GetList();
            }
        }


        public Stream GetAllMovies()
        {
            string returnString = string.Empty;
            Stream returnStream = null;
            using (var managerFactory = GetFactorySession())
            {
                var results = managerFactory.MovieManager.GetList();

                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, results);
                returnString = writer.ToString();

                returnStream = stringToStream(returnString);
            }

            return returnStream;
        }


        public Stream GetMovieById(string id)
        {
            var returnString = String.Empty;
           Stream returnStream = null;
            using (var managerFactory = GetFactorySession())
            {
                var results = managerFactory.MovieManager.GetMovieById(Convert.ToInt32(id));

                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, results);
                returnString = writer.ToString();

                returnStream = stringToStream(returnString);
            }

            return returnStream;
        }


        public Stream InsertMovie(MovieDTO movie)
        {
            string returnString = string.Empty;

            Stream returnStream = null;

            using (var managerFactory = GetFactorySession())
            {
                MovieDTO result = managerFactory.MovieManager.InserteMovie(movie);

                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, result);
                returnString = writer.ToString();

                returnStream = stringToStream(returnString);
            }

            return returnStream;
        }

        public string DeleteMovie(string id)
        {
            string returnString = string.Empty;

            using (var managerFactory = GetFactorySession())
            {
                 returnString = (managerFactory.MovieManager.DeleteMovie(Convert.ToInt32(id))).ToString();
            }

            return returnString;
        }

        public Stream UpdateMovie(MovieDTO movie)
        {
            string returnString = string.Empty;

            Stream returnStream = null;

            using (var managerFactory = GetFactorySession())
            {
                var result = managerFactory.MovieManager.UpdateMovie(movie);

                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, result);
                returnString = writer.ToString();

                returnStream = stringToStream(returnString);
            }

            return returnStream;
        }

        // USER
        public Stream InsertUser(UserDTO user)
        {
            string returnString = string.Empty;

            Stream returnStream = null;

            using (var managerFactory = GetFactorySession())
            {
                UserDTO result = managerFactory.UserManager.InsertUser(user);

                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, result);
                returnString = writer.ToString();

                returnStream = stringToStream(returnString);
            }

            return returnStream;
        }

        public Stream GetUserByUserName(string userName)
        {
            string returnString = string.Empty;

            Stream returnStream = null;

            using (var managerFactory = GetFactorySession())
            {
                var result = managerFactory.UserManager.GetUserByUserName(userName);

                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, result);
                returnString = writer.ToString();

                returnStream = stringToStream(returnString);
            }

            return returnStream;
        }


        public Stream UpdateUser(UserDTO user)
        {
            string returnString = string.Empty;

            Stream returnStream = null;

            using (var managerFactory = GetFactorySession())
            {
                var result = managerFactory.UserManager.UpdateUser(user);

                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, result);
                returnString = writer.ToString();

                returnStream = stringToStream(returnString);
            }

            return returnStream;
        }

        //ACTOR

        public Stream GetAllActors()
        {
            string returnString = string.Empty;

            Stream returnStream = null;

            using (var managerFactory = GetFactorySession())
            {
                var results = managerFactory.ActorManager.GetList();

                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, results);
                returnString = writer.ToString();

                returnStream = stringToStream(returnString);
            }
            return returnStream;
        }


        //UTILS

        private Stream stringToStream(string input)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";

            return new MemoryStream(Encoding.UTF8.GetBytes(input));
        }


    }
}